package com.hutou.chuangke.fragmentnet;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.ksoichiro.android.observablescrollview.ObservableRecyclerView;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
import com.github.ksoichiro.android.observablescrollview.ScrollUtils;
import com.hutou.chuangke.Bean.FriendBean;
import com.hutou.chuangke.Bean.MvlistBean;
import com.hutou.chuangke.Bean.SoundBean;
import com.hutou.chuangke.R;
import com.hutou.chuangke.activity.VideoViewActivity;
import com.hutou.chuangke.fragment.BaseFragment;
import com.hutou.chuangke.widget.CircleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/3.
 */

public class AlbumMvsFragment extends BaseFragment{
    private ArrayList<SoundBean> mList = new ArrayList<>();
    private GridLayoutManager gridLayoutManager;
    private AlbumMvsAdapter mvsAdapter;

    public static Fragment getInstance(ArrayList<SoundBean> mList){
        AlbumMvsFragment fragment = new AlbumMvsFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("list",mList);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static final String ARG_INITIAL_POSITION = "ARG_INITIAL_POSITION";
    ObservableRecyclerView recyclerView;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recyclerview, container, false);

        if (getArguments() != null) {
            mList = (ArrayList<SoundBean>) getArguments().getSerializable("list");
        }
        Activity parentActivity = getActivity();
        recyclerView = (ObservableRecyclerView) view.findViewById(R.id.scroll);
        gridLayoutManager = new GridLayoutManager(mContext, 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setHasFixedSize(true);
        mvsAdapter = new AlbumMvsAdapter(mList);
        recyclerView.setAdapter(mvsAdapter);

        if (parentActivity instanceof ObservableScrollViewCallbacks) {
            // Scroll to the specified offset after layout
            Bundle args = getArguments();
            if (args != null && args.containsKey(ARG_INITIAL_POSITION)) {
                final int initialPosition = args.getInt(ARG_INITIAL_POSITION, 0);
                ScrollUtils.addOnGlobalLayoutListener(recyclerView, new Runnable() {
                    @Override
                    public void run() {
                        //  recyclerView.scrollVerticallyToPosition(0);
                    }
                });
            }

            // TouchInterceptionViewGroup should be a parent view other than ViewPager.
            // This is a workaround for the issue #117:
            // https://github.com/ksoichiro/Android-ObservableScrollView/issues/117
            recyclerView.setTouchInterceptionViewGroup((ViewGroup) parentActivity.findViewById(R.id.root));

            recyclerView.setScrollViewCallbacks((ObservableScrollViewCallbacks) parentActivity);
        }
        return view;
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }

    class AlbumMvsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
        private ArrayList<SoundBean> mData = new ArrayList<>();
        private FriendsListFragment.OnItemClickListener mOnItemClickListener;

        public AlbumMvsAdapter(ArrayList<SoundBean> list){
            this.mData = list;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            AlbumMvsAdapter.MvViewHolder holder = new AlbumMvsAdapter.MvViewHolder(LayoutInflater.from(getActivity())
                    .inflate(R.layout.index_gridview_item,null));
            return holder;
        }

        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
            ((MvViewHolder)holder).grid_title.setText(mData.get(position).getSoundname());
            ((MvViewHolder)holder).grid_subtitle.setText(mData.get(position).getSinger());
            if(position == 0||position == 1) {
                ((MvViewHolder) holder).top_layout.setVisibility(View.VISIBLE);
            }

//            if(mData.get(position).getSex().equals("m")){
//                ((MvViewHolder)holder).sex_iv.setImageDrawable(getResources().getDrawable(R.drawable.me_male));
//            }else{
//                ((MvViewHolder)holder).sex_iv.setImageDrawable(getResources().getDrawable(R.drawable.me_female));
//            }

            ImageLoader.getInstance().displayImage(mData.get(position).getSoundurlimg(), ((MvViewHolder)holder).grid_image);
        }

        public void setData(ArrayList<SoundBean> mData){
            this.mData = mData;
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        class MvViewHolder extends RecyclerView.ViewHolder{
            TextView grid_title,grid_subtitle;
            ImageView grid_image;
            LinearLayout top_layout;
            public MvViewHolder(View view) {
                super(view);
                grid_title = (TextView) view.findViewById(R.id.grid_title);
                grid_subtitle = (TextView) view.findViewById(R.id.grid_subtitle);
                top_layout = (LinearLayout) view.findViewById(R.id.top_layout);
                grid_image = (ImageView) view.findViewById(R.id.grid_image);

                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getActivity(), VideoViewActivity.class);
                        intent.putExtra("mvurl",mData.get(getAdapterPosition()).getSoundurl());
                        startActivity(intent);
                    }
                });
            }
        }
    }
}
